<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once BASE_PATH . 'application/controllers/base' . EXT;

class Articulo extends Base {

    public function __construct() {
        parent::__construct();
        $this->load->model('itemDAO');
        $this->load->model('moduleDAO');
        $this->load->model('paginationDAO');
        $this->load->model('adsDAO');
        $this->load->model("sessionDAO");
        $this->load->model("suscriptoresDAO");
    }

    public function index($id = 1) {

        $articulo = $this->itemDAO->_findById($id);

        if (count($articulo) == 0)
            redirect('/');

        $this->itemDAO->setHit($id);

        /*
          Modulos
         */
        $estasemana = $this->moduleDAO->getParamsByStringId("home_estasemana");
        $relacionados = $this->itemDAO->findTagsById($id, array('limit' => array(0, 9)));

        /*
          Publicidad
         */

        $publicidadheader2 = $this->adsDAO->getAdsense("articulo_publicidadheaderright");
        $publicidadheader1 = $this->adsDAO->getAdsense("articulo_publicidadheaderleft");

        $publicidadboxbanner1 = $this->adsDAO->getAdsense("articulo_publicidadboxbanner1");
        $publicidadboxbanner2 = $this->adsDAO->getAdsense("articulo_publicidadboxbanner2");
        $publicidadboxbanner3 = $this->adsDAO->getAdsense("articulo_publicidadboxbanner3");
        $publicidadboxskyscraper1 = $this->adsDAO->getAdsense("articulo_publicidadboxskyscraper1");
        $publicidadfooterleft = $this->adsDAO->getAdsense("articulo_publicidadfooterleft");
        $publicidadfooterright = $this->adsDAO->getAdsense("articulo_publicidadfooterright");

        $correopass = $this->input->get('item');
        $data = array(
            "header" => array(
                "title" => "Artículo",
                "subtitle" => "",
                "description" => "",
                "correopass" => $correopass,
                "keywords" => "",
                "session" => $this->sessionDAO->getUser(),
                "publicidadheader2" => $publicidadheader2,
                "publicidadheader1" => $publicidadheader1,
            ),
            // Modulos
            "articulo" => $articulo,
            "estasemana" => $estasemana,
            "relacionados" => $relacionados,
            // Publicidad
            "publicidadboxbanner1" => $publicidadboxbanner1,
            "publicidadboxbanner2" => $publicidadboxbanner2,
            "publicidadboxbanner3" => $publicidadboxbanner3,
            "publicidadboxskyscraper1" => $publicidadboxskyscraper1,
            "publicidadfooterleft" => $publicidadfooterleft,
            "publicidadfooterright" => $publicidadfooterright,
        );

        $this->load->view('articulo/articulo', $data);
    }

    public function imagen($id = 1, $size = '') {
        $this->load->model("imageDAO");

        if (!empty($size)) {
            $data = array(
                "width" => $size,
                "image" => "media/k2/items/src/" . md5("Image" . $id) . ".jpg"
            );

            if (substr_count($size, "x") > 0) {

                $size = explode("x", $size);
                $data = array(
                    "width" => $size[0],
                    "height" => $size[1],
                    "image" => "media/k2/items/src/" . md5("Image" . $id) . ".jpg"
                );
            }
        } else {
            $data = array(
                "image" => "media/k2/items/src/" . md5("Image" . $id) . ".jpg"
            );
        }

        $this->imageDAO->convert($data);
    }

    public function galeria($galeriaNum, $img, $size) {

        $this->load->model("imageDAO");

        if (!empty($size)) {
            if (substr_count($size, "x") > 0) {
                $size = explode("x", $size);
                $data = array(
                    "width" => $size[0],
                    "height" => $size[1],
                );
            } else {
                $data = array(
                    "width" => $size,
                );
            }
        }

        $data["image"] = "media/k2/galleries/" . $galeriaNum . "/" . $img;

        $this->imageDAO->convert($data);
    }

}